<template>
  <div class="mod-user">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <!-- <el-input
          v-model="dataForm.userID"
          placeholder="单据编号"
          clearable
        ></el-input> -->
        <el-select
          v-model="dataForm.value"
          placeholder="请选择"
          @visible-change="checked($event)"
          @change="change($event)"
        >
          <el-option label="全选" value="全选"></el-option>
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="dataForm.name"
          placeholder="名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="dataForm.model"
          placeholder="型号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <div class="block">
          <el-date-picker
            v-model="dataForm.time"
            type="month"
            placeholder="选择月"
            @change="checkmonth($event)"
          >
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button type="success"
          ><i class="el-icon-download"></i>导出</el-button
        >
        <el-button type="primary"
          ><i class="el-icon-printer"></i>打印</el-button
        > -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="MaterialName"
        header-align="center"
        align="center"
        label="名称"
      >
      </el-table-column>
      <el-table-column
        prop="MaterialModel"
        header-align="center"
        align="center"
        label="型号"
      >
      </el-table-column>
      <el-table-column
        prop="MaterialOther"
        header-align="center"
        align="center"
        label="扩展信息"
      >
      </el-table-column>
      <el-table-column
        prop="unitName"
        header-align="center"
        align="center"
        width="180"
        label="单位"
      >
      </el-table-column>
      <el-table-column
        prop="UnitPrice"
        header-align="center"
        align="center"
        label="单价"
      >
      </el-table-column>
      <el-table-column
        prop="prevSum"
        header-align="center"
        align="center"
        label="上月结存数量"
      >
      </el-table-column>
      <el-table-column
        prop="InSum"
        header-align="center"
        align="center"
        label="入库数量"
      >
      </el-table-column>
      <el-table-column
        prop="OutSum"
        header-align="center"
        align="center"
        label="出库数量"
      >
      </el-table-column>
      <el-table-column
        prop="thisSum"
        header-align="center"
        align="center"
        label="本月结存"
      >
      </el-table-column>
      <el-table-column
        prop="thisAllPrice"
        header-align="center"
        align="center"
        label="结存金额"
      >
      </el-table-column>
      <!-- <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('sys:user:update')"
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
            >修改</el-button
          >
          <el-button
            v-if="isAuth('sys:user:delete')"
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./inoutstockreport-add-or-update copy";
export default {
  data() {
    return {
      dataForm: {
        value: "",
        name: "",
        model: "",
        time: new Date().getFullYear() + "-" + (new Date().getMonth() + 1),
        id: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      options: [],
      alldepotId: [],
    };
  },
  components: {
    AddOrUpdate,
  },
  // created(){
  //   this.getware()
  // },
  activated() {
    this.getDataList();
  },
  methods: {
    getTime() {
      this.time = this.timestampToTime(this.value1.getTime());
      console.log(this.time);
    },
    // 时间戳转成时间
    timestampToTime(timestamp) {
      var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + "-";
      var M =
        date.getHours() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1;
      return Y + M;
    },
    checkmonth(m) {
      // console.log(m);
      this.dataForm.time = this.timestampToTime(m.getTime());
      console.log(this.dataForm.time);
    },
    change(id) {
      // console.log(id)
      this.dataForm.id = id;
    },
    checked(e) {
      if (e) {
        this.getware();
      }
    },
    getware() {
      this.$http({
        url: this.$http.adornUrl("/depot/getAllList"),
        method: "get",
      }).then(({ data }) => {
        // console.log(data.data);
        this.alldepotId = [];
        data.data.map((item) => {
          // console.log(item)
          this.alldepotId.push(item.id);
        });
        console.log(this.alldepotId.toString());
        if (data && data.code === 0) {
          this.options = data.data;
          this.totalPage = data.data.totalCount;
        }
      });
    },
    // 获取数据列表
    getDataList() {
      this.getware();
      if (
        this.dataForm.time >
        new Date().getFullYear() + "-" + (new Date().getMonth() + 1)
      ) {
        console.log(111);
        this.$message({
          message: "选中时间不能大于当前时间，请重新选择",
          type: "warning",
          duration: 2000,
        });
        this.dataForm.time =
          new Date().getFullYear() + "-" + (new Date().getMonth() + 1);
      }
      console.log(this.dataForm.id);
      if (this.dataForm.id == "全选") {
        this.dataForm.id = "";
      }
      this.$http({
        url: this.$http.adornUrl("/depot/getAllList"),
        method: "get",
      }).then(({ data }) => {
        // console.log(data.data);
        this.alldepotId = [];
        data.data.map((item) => {
          // console.log(item)
          this.alldepotId.push(item.id);
        });
      });
      console.log(this.alldepotId.toString());
      this.dataListLoading = true;
      let currentPage = this.pageIndex || 1;
      let pageSize = this.pageSize || 10;
      // let depotId = this.dataForm.id||this.alldepotId;
      let depotId = this.dataForm.id;
      let monthTime = this.dataForm.time;
      let mpList = "规格,颜色,制造商";
      let name = this.dataForm.name || "";
      let model = this.dataForm.model || "";
      let data = this.$http.adornData(
        { currentPage, pageSize, depotId, monthTime, mpList, name, model },
        false
      );
      console.log(this.dataForm.id);
      let params = new FormData();
      // console.log(depotId);
      params.append("currentPage", currentPage);
      params.append("pageSize", pageSize);
      params.append("depotId", depotId);
      params.append("monthTime", monthTime);
      params.append("mpList", mpList);
      params.append("name", name);
      params.append("model", model);
      // console.log(data);
      this.$http({
        url: this.$http.adornUrl("/depotItem/findByAll"),
        method: "post",
        data: params,
      }).then(({ data }) => {
        // console.log(data.data.rows);
        if (data && data.code === 0) {
          this.dataList = data.data.rows;
          this.totalPage = data.data.rows.length;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      // console.log(id)
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var userIds = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.userId;
          });
      this.$confirm(
        `确定对[id=${userIds.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sys/user/delete"),
            method: "post",
            data: this.$http.adornData(userIds, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },

    // 重置
    resetDataList() {
      // console.log(11)
      this.dataForm.userName = "";
      this.dataForm.userID = "";
      this.value1 = "";
      this.getDataList();
    },
  },
};
</script>
